<template>
  <div class="main-container">
    <el-card class="box-card !border-none" shadow="never">
      <div class="flex justify-between items-center">
        <span class="text-lg">{{ pageName }}</span>
      </div>

      <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
        <el-form
          :inline="true"
          :model="memberPointsTable.searchParam"
          ref="searchFormRef"
        >
          <el-form-item :label="t('username')" prop="username">
            <el-input v-model="memberPointsTable.searchParam.username" placeholder="请输入" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loadMemberPointsList()">{{
              t("search")
            }}</el-button>
            <el-button @click="resetForm(searchFormRef)">{{ t("reset") }}</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <div class="mt-[10px]">
        <el-table
          :data="memberPointsTable.data"
          size="large"
          v-loading="memberPointsTable.loading"
        >
          <template #empty>
            <span>{{ !memberPointsTable.loading ? t("emptyData") : "" }}</span>
          </template>
          <el-table-column
            prop="username"
            :label="t('username')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="contribution_points"
            :label="t('contributionPoints')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="available_points"
            :label="t('availablePoints')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="gold_coupon"
            :label="t('goldCoupon')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column :label="t('operation')" fixed="right" min-width="120">
            <template #default="{ row }">
              <el-button type="primary" link @click="deleteEvent(row.id)">{{
                t("delete")
              }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt-[16px] flex justify-end">
          <el-pagination
            v-model:current-page="memberPointsTable.page"
            v-model:page-size="memberPointsTable.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="memberPointsTable.total"
            @size-change="loadMemberPointsList()"
            @current-change="loadMemberPointsList"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { t } from "@/lang";
import { getMemberPointsList, deleteMemberPoints } from "@/app/api/distributor";
import { ElMessageBox, FormInstance } from "element-plus";
import { useRoute } from "vue-router";
const route = useRoute();
const pageName = route.meta.title;

const memberPointsTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {
    username: "",
  },
});

const searchFormRef = ref<FormInstance>();

/**
 * 获取用户积分账户列表
 */
const loadMemberPointsList = (page: number = 1) => {
  memberPointsTable.loading = true;
  memberPointsTable.page = page;

  getMemberPointsList({
    page: memberPointsTable.page,
    limit: memberPointsTable.limit,
    ...memberPointsTable.searchParam,
  })
    .then((res) => {
      memberPointsTable.loading = false;
      memberPointsTable.data = res.data.data;
      memberPointsTable.total = res.data.total;
    })
    .catch(() => {
      memberPointsTable.loading = false;
    });
};
loadMemberPointsList();

/**
 * 删除用户积分账户
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t("memberPointsDeleteTips"), t("warning"), {
    confirmButtonText: t("confirm"),
    cancelButtonText: t("cancel"),
    type: "warning",
  }).then(() => {
    deleteMemberPoints(id)
      .then(() => {
        loadMemberPointsList();
      })
      .catch(() => {});
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  loadMemberPointsList();
};
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
  word-break: break-all;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
